<?php

namespace App\Http\Controllers\v1\users;

use App\Common\HeatCache\UserHeat;
use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\ValidationException;

class UserController extends Controller
{
    /**
     * 用户信息
     * @return false|string
     */
    public function userInfo()
    {
//        $this->userInfo->user_mobile = mobileDesens($this->userInfo->user_mobile);
//        $this->userInfo->user_email = emailDesens($this->userInfo->user_email);
        return $this->success(UserHeat::getUserInfoById(Auth::id()));
    }

    /**
     * 修改个人信息
     * @return false|JsonResponse|string
     * @throws ValidationException
     */
    public function upUserInfo()
    {
        $this->validate(request(),[
            'username'  => 'required|max:60',
            'email'     => 'required|email',
            'mobile'    => 'required',
        ]);

        [$userName,$userMobile,$userEmail] = array_values(request()->only(['username','mobile','email']));
        if (check_mobile($userMobile) === false) {
            return $this->error(50022);
        }

        $this->userInfo->user_name   = $userName;
        $this->userInfo->user_email  = $userEmail;
        $this->userInfo->user_mobile = $userMobile;
        if (!$this->userInfo->save()) {
            return $this->error();
        }
        return $this->success();
    }
}